/**
 * 说明：Android基础使用
 * 创建：雨中磐石  from www.rili123.cn
 * 时间：2014-11-28
 * 邮箱：qqqq2900@126.com
 * QQ：290802026/1073744729
 * */

package com.framerock.baselib;


import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Rock{

	public static boolean isEmpt(String str)
	{
		boolean bo = false;
		if(str == null)bo=true;
		if(!bo)if(str.length()==0 || str == "" || str.equals(""))bo = true;
		return bo;
	}

	public static boolean equals(String str, String instr)
	{
		if(str==null)str = "";
		if(instr==null)instr = "";
		return str.equals(instr);
	}
	
	public static boolean contain(String str, String instr)
	{
		boolean  bo = false;
		if(!isEmpt(str) && !isEmpt(instr)){
			if(str.indexOf(instr)>-1){
				bo = true;
			}
		}
		return bo;
	}

	public static Map<String, String> getMap(String[] params)
	{
		Map<String, String> amap= new HashMap<String, String>();
		if(params!=null){
			int len = params.length;
			for(int i=0;i< len-1;i=i+2){
				amap.put(params[i], params[i+1]);
			}
		}
		return amap;
	}

	/**
	 * 定义一个Map
	 * */
	public static Map<String, String> getMap()
	{
		return getMap("");
	}

	/**
	 * 合并两个Map
	 * */
	public static Map<String, String> joinMap(Map<String, String> map, Map<String, String> mapnew)
	{
		if(mapnew != null)for (Map.Entry<String, String> entry : mapnew.entrySet()) {
			map.put(entry.getKey(), entry.getValue());
		}
		return map;
	}

	/**
	 * 加数据
	 * */
	public static Map<String, String> putMap(Map<String, String> map, String key, String val)
	{
		if(map == null)map = getMap();
		map.put(key, val);
		return map;
	}


	/**
	 * 参数格式：23=33,33=22
	 */
	public static Map<String, String> getMap(String params)
	{
		Map<String, String> amap= new HashMap<String, String>();
		if(isEmpt(params))return amap;
		String arr[] = params.split(","),arr1[],val;
		for(int i=0;i<arr.length;i++){
			arr1 = arr[i].split("=");
			val  = "";
			if(arr1.length>1)val = arr1[1];
			amap.put(arr1[0], val);
		}
		return amap;
	}

	/**
	 * 获取Map下的值
	 * @param amap 对应Map
	 * @param key 键值
	 * @param dev 默认值
	 * */
	public static String getMapString(Map<String, String> amap, String key, String dev)
	{
		if(amap==null)return dev;
		String val = amap.get(key);
		if(isEmpt(val))val = dev;
		return val;
	}

	/**
	 * 获取Map下的值
	 * @param amap 对应Map
	 * @param key 键值
	 * */
	public static String getMapString(Map<String, String> amap, String key)
	{
		return getMapString(amap, key, "");
	}

	/**
	 * 获取Map下的数字
	 * @param amap 对应Map
	 * @param key 键值
	 * */
	public static int getMapint(Map<String, String> amap, String key)
	{
		int val = 0;
		String str = getMapString(amap, key);
		if(!Rock.isEmpt(str))val = Integer.parseInt(str);
		return val;
	}

	/**
	 * 是否包含在数组里
	 * */
	public static Boolean inArray(String arr[], String str)
	{
		Boolean bo = false;
		for(int i=0;i<arr.length;i++){
			if(arr[i].equals(str)){
				bo = true;
				break;
			}
		}
		return bo;
	}

	/**
	 * 获取创建一个数组
	 * */
	public static ArrayList<Map<String, String>> getMapList()
	{
		return new ArrayList<Map<String, String>>();
	}

	/**
	 * 是否包含为空全部
	 * */
	public static Boolean isInreceid(String inpath, String receid)
	{
		Boolean bo   = false;
		if(isEmpt(receid))return true;
		String arr[] = receid.split(",");
		for(int i=0;i<arr.length;i++){
			if(contain(","+inpath+",",","+arr[i]+",")){
				bo = true;
				break;
			}
		}
		return bo;
	}

	/**
	 * 数据替换
	 * */
	public static String replaceString(String str, Map<String, String> da)
	{
		if(str.indexOf("{")==-1 && str.indexOf("}")==-1)return str;
		Pattern p = Pattern.compile("\\{(.*?)\\}");
		Matcher m = p.matcher(str);
		String neirs = "",fkey,val;
		while (m.find()) {
			neirs = m.group();
			fkey  = neirs.substring(1, neirs.length()-1);
			val   = getMapString(da, fkey);
			str   = str.replace(neirs, val);
		}
		return str;
	}

	/**
	 * 获取进程ID
	 * */
	public static String getProcessId()
	{
		String name = ManagementFactory.getRuntimeMXBean().getName();
		int index = name.indexOf('@');
		if (index > 0) {
			return name.substring(0, index);
		}
		return name;
	}
}